home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 002 / emacssrc.arc / REGION.C < prev    next >
C/C++ Source or Header  |  1986-11-24  |  7KB  |  211 lines

  1. /*
  2.  * The routines in this file
  3.  * deal with the region, that magic space
  4.  * between "." and mark. Some functions are
  5.  * commands. Some functions are just for
  6.  * internal use.
  7.  */
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include        "edef.h"
  11.  
  12. #if    MEGAMAX & ST520
  13. overlay    "region"
  14. #endif
  15.  
  16. /*
  17.  * Kill the region. Ask "getregion"
  18.  * to figure out the bounds of the region.
  19.  * Move "." to the start, and kill the characters.
  20.  * Bound to "C-W".
  21.  */
  22. killregion(f, n)
  23. {
  24.         register int    s;
  25.         REGION          region;
  26.  
  27.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  28.         return(rdonly());    /* we are in read only mode    */
  29.         if ((s=getregion(®ion)) != TRUE)
  30.                 return (s);
  31.         if ((lastflag&CFKILL) == 0)             /* This is a kill type  */
  32.                 kdelete();                      /* command, so do magic */
  33.         thisflag |= CFKILL;                     /* kill buffer stuff.   */
  34.         curwp->w_dotp = region.r_linep;
  35.         curwp->w_doto = region.r_offset;
  36.         return (ldelete(region.r_size, TRUE));
  37. }
  38.  
  39. /*
  40.  * Copy all of the characters in the
  41.  * region to the kill buffer. Don't move dot
  42.  * at all. This is a bit like a kill region followed
  43.  * by a yank. Bound to "M-W".
  44.  */
  45. copyregion(f, n)
  46. {
  47.         register LINE   *linep;
  48.         register int    loffs;
  49.         register int    s;
  50.         REGION          region;
  51.  
  52.         if ((s=getregion(®ion)) != TRUE)
  53.                 return (s);
  54.         if ((lastflag&CFKILL) == 0)             /* Kill type command.   */
  55.                 kdelete();
  56.         thisflag |= CFKILL;
  57.         linep = region.r_linep;                 /* Current line.        */
  58.         loffs = region.r_offset;                /* Current offset.      */
  59.         while (region.r_size--) {
  60.                 if (loffs == llength(linep)) {  /* End of line.         */
  61.                         if ((s=kinsert('\n')) != TRUE)
  62.                                 return (s);
  63.                         linep = lforw(linep);
  64.                         loffs = 0;
  65.                 } else {                        /* Middle of line.      */
  66.                         if ((s=kinsert(lgetc(linep, loffs))) != TRUE)
  67.                                 return (s);
  68.                         ++loffs;
  69.                 }
  70.         }
  71.         return (TRUE);
  72. }
  73.  
  74. /*
  75.  * Lower case region. Zap all of the upper
  76.  * case characters in the region to lower case. Use
  77.  * the region code to set the limits. Scan the buffer,
  78.  * doing the changes. Call "lchange" to ensure that
  79.  * redisplay is done in all buffers. Bound to
  80.  * "C-X C-L".
  81.  */
  82. lowerregion(f, n)
  83. {
  84.         register LINE   *linep;
  85.         register int    loffs;
  86.         register int    c;
  87.         register int    s;
  88.         REGION          region;
  89.  
  90.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  91.         return(rdonly());    /* we are in read only mode    */
  92.         if ((s=getregion(®ion)) != TRUE)
  93.                 return (s);
  94.         lchange(WFHARD);
  95.         linep = region.r_linep;
  96.         loffs = region.r_offset;
  97.         while (region.r_size--) {
  98.                 if (loffs == llength(linep)) {
  99.                         linep = lforw(linep);
  100.                         loffs = 0;
  101.                 } else {
  102.                         c = lgetc(linep, loffs);
  103.                         if (c>='A' && c<='Z')
  104.                                 lputc(linep, loffs, c+'a'-'A');
  105.                         ++loffs;
  106.                 }
  107.         }
  108.         return (TRUE);
  109. }
  110.  
  111. /*
  112.  * Upper case region. Zap all of the lower
  113.  * case characters in the region to upper case. Use
  114.  * the region code to set the limits. Scan the buffer,
  115.  * doing the changes. Call "lchange" to ensure that
  116.  * redisplay is done in all buffers. Bound to
  117.  * "C-X C-L".
  118.  */
  119. upperregion(f, n)
  120. {
  121.         register LINE   *linep;
  122.         register int    loffs;
  123.         register int    c;
  124.         register int    s;
  125.         REGION          region;
  126.  
  127.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  128.         return(rdonly());    /* we are in read only mode    */
  129.         if ((s=getregion(®ion)) != TRUE)
  130.                 return (s);
  131.         lchange(WFHARD);
  132.         linep = region.r_linep;
  133.         loffs = region.r_offset;
  134.         while (region.r_size--) {
  135.                 if (loffs == llength(linep)) {
  136.                         linep = lforw(linep);
  137.                         loffs = 0;
  138.                 } else {
  139.                         c = lgetc(linep, loffs);
  140.                         if (c>='a' && c<='z')
  141.                                 lputc(linep, loffs, c-'a'+'A');
  142.                         ++loffs;
  143.                 }
  144.         }
  145.         return (TRUE);
  146. }
  147.  
  148. /*
  149.  * This routine figures out the
  150.  * bounds of the region in the current window, and
  151.  * fills in the fields of the "REGION" structure pointed
  152.  * to by "rp". Because the dot and mark are usually very
  153.  * close together, we scan outward from dot looking for
  154.  * mark. This should save time. Return a standard code.
  155.  * Callers of this routine should be prepared to get
  156.  * an "ABORT" status; we might make this have the
  157.  * conform thing later.
  158.  */
  159. getregion(rp)
  160. register REGION *rp;
  161. {
  162.         register LINE   *flp;
  163.         register LINE   *blp;
  164.         long fsize;
  165.         long bsize;
  166.  
  167.         if (curwp->w_markp == NULL) {
  168.                 mlwrite("No mark set in this window");
  169.                 return (FALSE);
  170.         }
  171.         if (curwp->w_dotp == curwp->w_markp) {
  172.                 rp->r_linep = curwp->w_dotp;
  173.                 if (curwp->w_doto < curwp->w_marko) {
  174.                         rp->r_offset = curwp->w_doto;
  175.                         rp->r_size = (long)(curwp->w_marko-curwp->w_doto);
  176.                 } else {
  177.                         rp->r_offset = curwp->w_marko;
  178.                         rp->r_size = (long)(curwp->w_doto-curwp->w_marko);
  179.                 }
  180.                 return (TRUE);
  181.         }
  182.         blp = curwp->w_dotp;
  183.         bsize = (long)curwp->w_doto;
  184.         flp = curwp->w_dotp;
  185.         fsize = (long)(llength(flp)-curwp->w_doto+1);
  186.         while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) {
  187.                 if (flp != curbp->b_linep) {
  188.                         flp = lforw(flp);
  189.                         if (flp == curwp->w_markp) {
  190.                                 rp->r_linep = curwp->w_dotp;
  191.                                 rp->r_offset = curwp->w_doto;
  192.                                 rp->r_size = fsize+curwp->w_marko;
  193.                                 return (TRUE);
  194.                         }
  195.                         fsize += llength(flp)+1;
  196.                 }
  197.                 if (lback(blp) != curbp->b_linep) {
  198.                         blp = lback(blp);
  199.                         bsize += llength(blp)+1;
  200.                         if (blp == curwp->w_markp) {
  201.                                 rp->r_linep = blp;
  202.                                 rp->r_offset = curwp->w_marko;
  203.                                 rp->r_size = bsize - curwp->w_marko;
  204.                                 return (TRUE);
  205.                         }
  206.                 }
  207.         }
  208.         mlwrite("Bug: lost mark");
  209.         return (FALSE);
  210. }
  211.